<?php
	include_once ( "AbstractDAO.php" );
	
	class ListDAO extends AbstractDAO 
	{
		
		function GetListAuthorId( $listId )
		{
			$sql = "select user_id from list where id = $listId ";
			return $this->db->GetValue( $sql );
		}
		
		function GetListByLid($lid)
		{
			return $this->db->GetRow("select * from list where id=".$lid);
		}

		function GetListByID($lid)
		{		 
	        $sql = "select 	list.id, list.user_id, list.title, list.description, list.create_time,
	        				list.modify_time,tags, list.view_count, list.response_count, list.flower_count,
	        				list.egg_count, list.c2c_count, list.b2c_count, list.product_count,
	                		us.contribution as contribution,
					        ubi.nick as user_name,
					       	ubi.logo_path as user_pic
			          from 	list,
			             	user_statistic us,
			             	user_basic_info ubi
			         where 	us.user_id = list.user_id
			           and 	ubi.id = list.user_id
			           and 	list.id = $lid";	 
	        
			$result = $this->db->GetRow($sql);
			return $result;	
		}
		
		function GetListByUid($uid, $count='20')
		{
			$sql="select id, title from list where user_id=".$uid." order by create_time desc limit 0,".$count;
			$result=$this->db->GetPlan($sql);
			return $result;
		}
		
		function GetListBySid($sid,$num='5'){

			// OPT by yawei
			$sql = "select l.id, l.title  
					from list_entry e
					left join list l 
					on (l.id = e.list_id)
					where e.entry_type<>3 and e.entry_id = $sid limit 0, $num";
			
	        //$sql = "select l.id,l.title
			//        from list l,list_entry e where l.id = e.list_id and e.entry_type<>3 and e.entry_id = $sid limit 0,".$num;
			 
			$result = $this->db->GetPlan($sql);
				
			return $result;	
		}
		
		function GetListByPid($pid,$num='5'){

			// OPT by yawei
			$sql = "select l.id, l.title  
					from list_entry e
					left join list l 
					on (l.id = e.list_id)
					where e.entry_type = 3 and e.entry_id = $pid limit 0, $num";
			
	        //$sql = "select l.id,l.title
			//        from list l,list_entry e where l.id = e.list_id and e.entry_type=3 and e.entry_id = $pid limit 0,".$num;
			 
			$result = $this->db->GetPlan($sql);
				
			return $result;	
		}		
		
		function GetListBySortAndFilter($sorttype,$tag,$keyword,$perpage,$pagenum)
		{	
			if(!edp_is_empty_string($tag))
			{
					
				 $num = $this->db->GetValue("select count(*) from list where tags like '%$tag%'");
			 	
				 $sql = "select list.*, 
				       		 		us.contribution as contribution,
				       				ubi.nick as user_name
				        	   from list,
						             user_statistic us,
						             user_basic_info ubi
					        where list.user_id = us.user_id
					          and list.user_id = ubi.id
					          and list.tags like '%$tag%'"; 
			} 
			else {
				if(!edp_is_empty_string($keyword))
				{
				
				 	$num = $this->db->GetValue("select count(*) from list where title like '%$keyword%' or description like '%$keyword%'");			 
				 
					$sql = "select list.*, 
			       		 		us.contribution as contribution,
			       				ubi.nick as user_name
			        	   from list,
					             user_statistic us,
					             user_basic_info ubi
				        where list.user_id = us.user_id
				          and list.user_id = ubi.id	       
				          and ( list.title like '%$keyword%' or list.description like '%$keyword%' )";
				}
				else 
				{
	
					$num = $this->db->GetValue("select count(*) from list");
				 
				 	$sql = "select list.*, 
			       		 		us.contribution as contribution,
			       				ubi.nick as user_name
			        	   from list,
					             user_statistic us,
					             user_basic_info ubi
				        where list.user_id = us.user_id
				          and list.user_id = ubi.id";
				}
			}
			
			$result=array();
			$result['total_num']=$num;
			
		    if($num!='0')
		    {	
		     	if($sorttype=='create_time'||$sorttype=='response_count'||$sorttype=='flower_count'||
				 $sorttype=='view_count')
				 {
					 $sql=$sql." order by list.$sorttype desc";
				 }
				 elseif($sorttype=='contribution')
				 {
				 	$sql=$sql." order by us.$sorttype desc";
				 }
		    	$result['list_summary']=$this->GetRows($sql,$perpage,$pagenum);
		    }
			
			return $result;	
		}		
		
		function GetHotList($count= 5)
		{
			$cacheManager = GetCacheManager();
			$re = $cacheManager->Get( "hotList_".$count, CNS_LIST );
			if( $re !== false )
				return $re;
				
			$sql="select id, title, create_time,user_id,(select nick from user_basic_info u where u.id=l.user_id ) as nick from list l 
			force index(list_view_count) order by view_count desc limit 0,".$count;
			return $this->db->GetPlan($sql, true, CNS_LIST, "hotList_".$count);
		}
		
		
		function GetRelatedList($count = 5)
		{
			$cacheManager = GetCacheManager();
			$re = $cacheManager->Get( "relatedList_".$count, CNS_LIST );
			if( $re !== false )
				return $re;
				
			$sql="select id, title, create_time, user_id, pic_path,
			(select nick from user_basic_info u where u.id=l.user_id ) as nick from list l
			force index(list_view_count) order by view_count desc limit 0,".$count;
			return $this->db->GetPlan($sql, true, CNS_LIST, "relatedList_".$count);
		}
						
		function GetResponseByLid($lid)
		{
			$num = $this->db->GetValue("select response_count from list where id =$lid");	
			// seal modified "order by date desc"		 	
			$sql = "select * from list_response where list_id =$lid order by id desc";
            $result=array();
			$result['total_num']=$num;
		    if($num!='0')	
		    	$result['result']=$this->db->GetPlan($sql);
			return $result;	
		}
						
	
		function GetEntryReview($eid,$type,$uid)
		{	
//			Need to decide whether to use this
//
//          if($type == '3'){
//			}else{
//				$sql ="select id from review where shop_id=$eid and user_id=$uid order by review_date desc limit 0,1";										
//			}
//			
//			return  $this->db->GetValue($sql);
		}
	
		
		function UpdateListViewCount($lid){
			
			$sql = "update list set view_count = view_count +1 where id=".$lid;

			$this->db->Update($sql);
							
		}
		
	    function GetRecentList($num =10)
	    {
	    	$cacheManager = GetCacheManager();
			$re = $cacheManager->Get( "recentList_".$count, CNS_LIST );
			if( $re !== false )
				return $re;
				
	    	 $sql = "select id,user_id,title,pic_path,create_time,
	    	 (select nick from user_basic_info where id= list.user_id) as user_name
	    	  from list order by id desc limit 0,".$num;
	    	 
	    	 return $this->db->GetPlan($sql, true, CNS_LIST, "recentList_".$count); 
	    }
	    
		function GetHotTags($num = 0)
		{
			$cacheManager = GetCacheManager();
			$re = $cacheManager->Get( "hotListTag_".$num, CNS_LIST );
			if( $re !== false )
				return $re;
				
			if($num!='0')
			  $sql = "select tags from list limit 0,".$num;
			else 
			  $sql = "select tags from list";
		       
			$tempArr = $this->db->GetPlan($sql);
	       
			if($tempArr===false)
				return false;
			$hotTags1 =array();
			foreach($tempArr as $item){
				
				$hotTags1 = array_merge ( $hotTags1, explode(" ", trim($item['tags'])));	    
			
			}
			
			$hotTags2 = array();
			foreach( $hotTags1 as $item )
			{
				if( !isset($hotTags2["$item"]) ){
					$hotTags2["$item"] = 1;
				}
				else{
					$hotTags2["$item"] = intval($hotTags2["$item"])+1;
				}
			}
			arsort($hotTags2, SORT_NUMERIC);
		
			$cacheManager->Set( "hotListTag_".$num, $hotTags2, CNS_LIST );
			return $hotTags2;
	    }
	    
	    function CheckListFlowerStatus($uid, $lid){
		    	
		    $sql= "select * from list_flower where user_id = $uid and list_id = $lid";
	
			$result = $this->db->GetRow( $sql );
			
			if($result){
				//in database 
				return true;
				
			}else{
	            //not in database
				return false;
			}
		    	
	    }
	    	    
		function CheckListEggStatus($uid, $lid){
		    	
		    $sql= "select * from list_egg where user_id = $uid and list_id = $lid";
	
			$result = $this->db->GetRow( $sql );
			
			if($result){
				//in database 
				return true;
				
			}else{
	            //not in database
				return false;
			}		    	
	    }
	    
	    function AddListFlower($uid, $lid)
	    {
	    	$sql="select user_id from list where id=$lid";
	    	if($this->db->GetValue($sql)==$uid)
	    		return '0';
	    	else
	    	{
		    	$now=GetCurrentDate();	
		    	try
		    	{
		    		$this->db->BeginTransaction();
			    	$sql = "insert into 
							list_flower(user_id, list_id,date) values( $uid, $lid,'$now')";
					$this->db->Insert( $sql );	
					$sql = "update list set flower_count=flower_count+1 where id= $lid";
					$this->db->Update( $sql );	
					$sql = "update user_statistic set flower_count=flower_count+1 where user_id=(select user_id from 
					list where id=$lid)";
					$this->db->Update( $sql );	
					$this->db->Commit();
		    		return '1';
		    	}
		    	catch( Exception $e)
		    	{
			    	$this->db->Rollback();
			    	return '-1';
		    	}
		    	
	    	}
	    }
	    		    
	    function AddListEgg($uid, $lid)
	    {	    	    	
	    	$list_uid=$this->db->GetValue("select user_id from list where id=$lid");
	    	if(!$list_uid)
	    		return '-2';  
	    	else
	    	{		
		    	if($list_uid==$uid)
		    		return '0';
		    	else
		    	{
		    		$userDAO=GetUserDAO();	
		    		if($userDAO->InBlackList($list_uid, $uid))
		    			return '-1';	
		    		else
		    		{
				    	try
				    	{
				    		$now=GetCurrentDate();
				    		$this->db->BeginTransaction();
					    	$sql = "insert into 
									list_egg(user_id, list_id,date) values( $uid, $lid,'$now')";
							$this->db->Insert( $sql );	
							$sql = "update list set egg_count=egg_count+1 where id= $lid";
							$this->db->Update( $sql );	
							$sql = "update user_statistic set egg_count=egg_count+1 where user_id=(select user_id from 
							list where id=$lid)";
							$this->db->Update( $sql );	
							$this->db->Commit();
				    		return '1';
				    	}
				    	catch( Exception $e)
				    	{
					    	$this->db->Rollback();
					    	return '-2';
				    	}
				    	
		    		}
		    	}
	    	}
	    }
	    
	    
		function DeleteListFlower($uid, $lid)
		{	
			try
	    	{
	    		$this->db->BeginTransaction();
		        $sql = "delete from 
		                list_flower where user_id = $uid and list_id = $lid";	
				$this->db->Update( $sql );	
				$sql = "update list set flower_count=flower_count-1 where id= $lid";
				$this->db->Update( $sql );	
				$sql = "update user_statistic set flower_count=flower_count-1 where user_id=(select user_id from 
				list where id=$lid)";
				$this->db->Update( $sql );	
				$this->db->Commit();
	    		return '1';
			}
	    	catch( Exception $e)
	    	{
		    	$this->db->Rollback();
		    	return '-1';
	    	}
	    	
	    }
	    
		function DeleteListEgg($uid, $lid)
		{	
			try
			{
				$this->db->BeginTransaction();
		        $sql = "delete from 
		                list_egg where user_id = $uid and list_id = $lid";
				$this->db->Update( $sql );	
				$sql = "update list set egg_count=egg_count-1 where id= $lid";
				$this->db->Update( $sql );
				$sql = "update user_statistic set egg_count=egg_count-1 where user_id=(select user_id from 
				list where id=$lid)";
				$this->db->Update( $sql );
				$this->db->Commit();
	    		return '1';		
			}
	    	catch( Exception $e)
	    	{
		    	$this->db->Rollback();
		    	return '-1';
	    	}
	    		
	    }
	
	    function AddEntryResponse($lid, $list_uid, $userid, $userip, $anonymous, $response)
	    {
	    	$now=GetCurrentDate();
	    	$userDAO=GetUserDAO();
	    	if($userid>'0'&&$userDAO->InBlackList($list_uid, $userid)==true)
	    		return '0';
	    	else
	    	{
		    	try
				{
					$this->db->BeginTransaction();
			    	$sql = "insert into 
							list_response(list_id,user_id,user_ip,is_anonymous,date,content) values( $lid, $userid,'$userip',$anonymous,'$now','$response')";		
					$this->db->Insert( $sql );	
					$sql = "update list set response_count=response_count+1 where id= $lid";
					$this->db->Update( $sql );	
					$this->db->Commit();
		    		return '1';   
				}
		    	catch( Exception $e)
		    	{
			    	$this->db->Rollback();
			    	return '-1';
		    	}
		    	
	    	} 
	    }
	    
	    /**********************************************************
	     *	used for create list
	     *	Seal Created by 2010-2-2
	     **********************************************************/
	    
	    function AddNewList( $title, $desc, $label, $shop_list, $product_list, $list_array )
	    {
	    	if( count($list_array) == 0 )
	    		return true;
	    		
	    	$userid = GetLoginUserID();
	    	$time = GetCurrentDate();
	    	$firstId = $list_array[0]['id'];
	    	$firstType = $list_array[0]['category'];
	    	
	    	if( $firstType == '0' )
	    	{
	    		if( IsB2C($shop_list[$firstId]['plat_id']) )
	    			$first_entry_type = 2;
	    		else 
	    			$first_entry_type = 1;
	    			
	    		$first_entry_id = $firstId;
	    		$pic_path = $shop_list[$firstId]['logo_url'];
	    	}
	    	else
	    	{
	    		$first_entry_type = 3;
	    		$first_entry_id = $firstId;
	    		$pic_path = $product_list[$firstId]['pic_path'];
	    	}
	    	
	    	$c2c_count = 0;
	    	$b2c_count = 0;
	    	
	    	foreach( $shop_list as $k => $v )
	    	{
	    		if( IsB2C($v['plat_id']) )
	    			$b2c_count++;
	    		else if( IsC2C($v['plat_id']) )
	    			$c2c_count++;
	    	}
	    	
	    	$product_count = count( $product_list );
	    	
	    	$item_priority = '';
	    	
	    	foreach( $list_array as $k => $v )
	    	{
	    		$item_priority .= $v['id'] . ':' . $v['category'] . ' ';
	    	}
	    	
	    	$sql = "insert into list( user_id, title, description, create_time, modify_time, tags, pic_path, first_entry_type,
	    							  first_entry_id, view_count, response_count, flower_count, egg_count, c2c_count, b2c_count,
	    							  product_count, item_priority )
	    			values( $userid, '$title', '$desc', '$time', '$time', '$label', '$pic_path', $first_entry_type,
	    					$first_entry_id, 0, 0, 0, 0, $c2c_count, $b2c_count, $product_count, '$item_priority')";
	    	    	
	    	try
	    	{
	    		$this->db->BeginTransaction();
	    		$this->db->Insert( $sql );
	    		$list_id = $this->db->LastID();
	    	
	    		foreach( $shop_list as $k => $v )
	    		{
	    			// 1 is shop 
	    			// 2 is mall
	    			// 3 is product
	    			$entry_type = IsB2C($v['plat_id']) ? 2 : 1;
	    			$sql = "insert into list_entry( list_id, entry_type, entry_id, description)
	    					values( $list_id, $entry_type, " . $v['id'] . ", '')";
	    			$this->db->Insert( $sql );
	    		}
	    		
	    		foreach( $product_list as $k => $v )
	    		{
	    			$sql = "insert into list_entry( list_id, entry_type, entry_id, description)
	    					values( $list_id, 3, " . $v['id'] . ", '')";
	    			$this->db->Insert( $sql );
	    		}
	    		
	    		$sql=" update user_statistic set list_count = list_count + 1 where user_id = $userid ";
	    		$this->db->Update( $sql );
	    		$this->db->Commit();
	    		return $list_id;
	    	}
	    	catch( Exception $e)
	    	{
	    		$this->db->Rollback();
	    		return false;
	    	}
	    	
	    	
	    	
	    }
	    
	    function UpdateList( $list_id, $title, $desc, $label, $shop_list, 
	    	$product_list, $list_array, $add_items, $delete_items )
	    {
	    	if( count($list_array) == 0 )
	    		return true;
	    		
	    	$userid = GetLoginUserID();
	    	$time = GetCurrentDate();
	    	$firstId = $list_array[0]['id'];
	    	$firstType = $list_array[0]['category'];
	    	
	    	if( $firstType == '0' )
	    	{
	    		if( IsB2C($shop_list[$firstId]['plat_id']) )
	    			$first_entry_type = 2;
	    		else 
	    			$first_entry_type = 1;
	    			
	    		$first_entry_type = 1;
	    		$first_entry_id = $firstId;
	    		$pic_path = $shop_list[$firstId]['logo_url'];
	    	}
	    	else
	    	{
	    		$first_entry_type = 3;
	    		$first_entry_id = $firstId;
	    		$pic_path = $product_list[$firstId]['pic_path'];
	    	}
	    	
	    	$c2c_count = 0;
	    	$b2c_count = 0;
	    	
	    	foreach( $shop_list as $k => $v )
	    	{
	    		if( IsB2C($v['plat_id']) )
	    			$b2c_count++;
	    		else if( IsC2C($v['plat_id']) )
	    			$c2c_count++;
	    	}
	    	
	    	$product_count = count( $product_list );
	    	
	    	$item_priority = '';
	    	
	    	foreach( $list_array as $k => $v )
	    	{
	    		$item_priority .= $v['id'] . ':' . $v['category'] . ' ';
	    	}
	    	
	    	$sql = "update list
	    			set title = '$title',
	    				description = '$desc',
	    				modify_time = '$time', 
	    				tags = '$label', 
	    				pic_path = '$pic_path',
	    				first_entry_type = $first_entry_type,
	    				first_entry_id = $first_entry_id,
	    				c2c_count = $c2c_count,
	    				b2c_count = $b2c_count,
	    				product_count = $product_count,
	    				item_priority = '$item_priority'
	    			where id = $list_id";
	    			
	    	try
	    	{
	    		$this->db->BeginTransaction();
	    		$this->db->Update( $sql );
	    	
	    		foreach( $shop_list as $k => $v )
	    		{
	    			if( find_item_by_id($v['id'], $add_items, 0) == false )
	    				continue;
	    			$entry_type = IsB2C($v['plat_id']) ? 2 : 1;	
	    			$sql = "insert into list_entry( list_id, entry_type, entry_id, description)
	    					values( $list_id, $entry_type, " . $v['id'] . ", '')";
	    			$this->db->Insert( $sql );
	    		}
	    		
	    		foreach( $product_list as $k => $v )
	    		{
	    			if( find_item_by_id($v['id'], $add_items, 1) == false )
	    				continue;
	    			$sql = "insert into list_entry( list_id, entry_type, entry_id, description)
	    					values( $list_id, 3, " . $v['id'] . ", '')";
	    			$this->db->Insert( $sql );
	    		}
	
	    		if( count($delete_items) > 0 )
	    		{
	    			$delete_ids = implode( ',', $delete_items );
	    		
		    		$sql = "delete from list_entry
		    				where list_id = $list_id and entry_id in($delete_ids)";
		    		$this->db->Update( $sql );
	    		}
	    		$this->db->Commit();
	    		return true;
	    		
	    	}
	    	catch( Exception $e)
	    	{
	    		echo $e->getMessage();
	    		$this->db->Rollback();
	    		return false;
	    	}
	    	
	    	
	    	
	    }
	    
	    function GetListItemPriority( $list_id )
	    {
	    	$sql = "select item_priority
	    			from list
	    			where id = $list_id";
	    	
	    	return $this->db->GetValue( $sql );
	    }
		
	    function GetUserList( $user_id, $offset, $count = LIST_PER_PAGE, $orderby)
	    {
	    	$sql = "select id, title, description, create_time, tags, pic_path,
	    				   view_count, response_count, egg_count, flower_count,
	    				   c2c_count, b2c_count, product_count
	    			from list
	    			inner join( select p.id 
	    						from list p 
	    						where user_id=$user_id
	    						order by $orderby 
	    						limit $offset, $count
	    						) jt
	    			using(id)";
	    	return $this->db->GetPlan( $sql );
	    }
		
	    function GetUserListSummary( $list_id )
	    {
//	    	$sql = "select title, description, c2c_count+b2c_count+product_count total, item_priority
//	    			from list
//	    			where id = $list_id and user_id = $user_id";

	        $sql = "select user_id,title,description,create_time,modify_time,tags,c2c_count,b2c_count,product_count,
	                view_count,response_count,flower_count, egg_count,c2c_count+b2c_count+product_count total, item_priority, 
			       (select contribution from user_statistic where user_id= list.user_id) as contribution,
			       (select nick from user_basic_info where id= list.user_id) as user_name,
			       (select logo_path from user_basic_info where id= list.user_id) as user_pic
			        from list where id = $list_id ";//and user_id = $user_id";	 
			        	    	
	    	return $this->db->GetRow( $sql );
	    }
	    
	    function GetListEntry( $list_id, $offset, $count = LISTENTRY_PER_PAGE)
	    {
	    	$sql = "select id, description, entry_type, entry_id
	    			from list_entry
	    			inner join( select p.id 
	    						from list_entry p 
	    						where list_id = $list_id 
	    						order by p.id desc
	    						limit $offset, $count) jt
	    			using(id)";
	    	
	    	return  $this->db->GetPlan( $sql );
	    }
	    	
	    function UpdateEntryDescription( $entry_id, $content )
	    {
	    	$sql = "update list_entry
	    			set description = '$content'
	    			where id = $entry_id";
	    	$this->db->Update( $sql );
	    }
	    
	    function GetUidByEntry( $entry_id )
	    {
	    	$sql = "select l.user_id from list l, list_entry le where le.id=$entry_id and 
	    	le.list_id=l.id";
	    	return $this->db->GetValue($sql);
	    }
	    
	    
	    function GetHostList( $count = 3 )
	    {
	    	$cacheManager = GetCacheManager();
			$re = $cacheManager->Get( 'GetHotList_'.$count, CNS_INDEX );
			
			if( $re!= false )
				return $re;
			
				/*
			$searchEngine = GetListSearch();
			$ids = $searchEngine->GetHotList( $count );
			
			if( $ids === false )
				return false;
			
			$sql="select id, title, pic_path, description from list where id in( " . implode( ',', $ids ) . ")";	
			*/
			$sql="select id, title, pic_path, description from list order by response_count desc limit 0,".$count;
				
	    	return $this->db->GetPlan($sql, true, CNS_INDEX, 'GetHotList_'.$count);
	    }
	    
	    function GetRecommendList( $count = 3 )
	    {
	    	$cacheManager = GetCacheManager();
			$re = $cacheManager->Get( 'GetRecommendList', CNS_INDEX );
			
			if( $re!= false )
				return $re;
			
			$searchEngine = GetListSearch();
			$ids = $searchEngine->GetRecommendList( $count );
			
			if( $ids === false )
				return false;
			
			$sql="select id, title, pic_path, description from list where id in( " . implode( ',', $ids ) . ")";	
				
				
	    	return $this->SortByIdArray( $this->db->GetPlan($sql, true, CNS_INDEX, 'GetRecommendList'), $ids );
		
	    }
	    
	    function GetNewList( $count = 3 )
	    {
	    	$cacheManager = GetCacheManager();
                $re = $cacheManager->Get( 'GetNewList_'.$count, CNS_INDEX );

                if( $re!= false )
                        return $re;

                $searchEngine = GetListSearch();
                $ids = $searchEngine->GetNewList( $count );

                if( $ids === false )
                        return false;

                $sql="select id, title, pic_path, description from list where id in( " . implode( ',', $ids ) . ")";

                return $this->SortByIdArray( $this->db->GetPlan($sql), $ids );
	    }
	    
	    
	    function DeleteList( $listId, $userId, $coin )
	    {
		    try
		    	{
		    		$this->db->BeginTransaction();

		    		$sql = "select id from list where id = $listId and user_id = $userId for update";
		    		
		    		$id = $this->db->GetValue( $sql );
		    		
		    		if( $id === false ) 
		    			return false;
		    			
		    		// delete list egg
		    		$sql = "delete from list_egg where list_id = $listId ";
		    		$this->db->Update( $sql ); 
		    		
		    		// delete list flower
		    		$sql = "delete from list_flower where list_id = $listId ";
		    		$this->db->Update( $sql ); 

		    		// delete list response
		    		$sql = "delete from list_response where list_id = $listId ";
		    		$this->db->Update( $sql ); 

		    		// delete list entries
		    		$sql = "delete from list_entry where list_id = $listId ";
		    		$this->db->Update( $sql ); 
		    				    		
		    		// update the user statistic
		    		$sql=" update user_statistic 
		    				  set list_count = list_count - 1,
		    				  	  contribution = case when contribution > $coin then (contribution - $coin) else 0 end,
		    				  	  coins = case when coins > $coin then (coins - $coin) else 0 end
		    				where user_id = $userId ";
		    		
		    		$this->db->Update( $sql );
		    		
		    		// delete the list itself
		    		$sql = "delete from list where id = $listId and user_id = $userId ";
		    		$this->db->Update( $sql );
		    		
		    		$this->db->Commit();
		    		return true;
		    	}
		    	catch( Exception $e)
		    	{
		    		$this->db->Rollback();
		    		return false;
		    	}
	    }
	}
	
?>
